package com.cloud.logger.zchain.zchainserver.controller;

import com.cloud.logger.zchain.core.common.base.ResultMessage;
import com.cloud.logger.zchain.zchainserver.base.PagePo;
import com.cloud.logger.zchain.zchainserver.entity.MailFailure;
import com.cloud.logger.zchain.zchainserver.entity.MailSuccess;
import com.cloud.logger.zchain.zchainserver.mail.impl.MailFailureServiceImpl;
import com.cloud.logger.zchain.zchainserver.mail.impl.MailServiceImpl;
import com.cloud.logger.zchain.zchainserver.service.CheckProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 浙江卓锐科技股份有限公司
 *
 * @author zhengyt
 * @create 2017/11/9
 * @description
 */
@RestController
@RequestMapping("/mail")
@Api(value = "/邮件信息接口", description = "邮件信息增删查改，发送邮件等功能")
public class MailCtl {

    @Autowired
    private MailServiceImpl mailService;

    @Autowired
    private MailFailureServiceImpl mailFailService;

    @Resource
    private CheckProjectService checkProjectService;

    @ApiOperation(value = "保存邮件信息")
    @PostMapping(value = "/save")
    @ResponseBody
    public ResultMessage<MailSuccess> save(@RequestBody MailSuccess mailSuccess) {
        return mailService.save(mailSuccess);
    }

    @ApiOperation(value = "通过邮件id获取信息")
    @RequestMapping(value = "/{id}/{type}", method = RequestMethod.GET)
    public ResultMessage<MailSuccess> getMail(@PathVariable String id,@PathVariable String type) {
        return mailService.getMail(id,type);
    }

    @ApiOperation(value = "更新邮件信息")
    @PutMapping(value = "/update")
    @ResponseBody
    public ResultMessage<MailSuccess> update(@RequestBody MailSuccess mailSuccess) {
        return mailService.update(mailSuccess);
    }

    @ApiOperation(value = "通过邮件信息获取信息", response = MailSuccess.class)
    @PostMapping(value = "/getMailInfo")
    @ResponseBody
    public ResultMessage getMailInfo(@RequestBody MailSuccess mailSuccess) {
        return mailService.select(mailSuccess);
    }

    @ApiOperation(value = "通过邮件id删除信息", response = MailSuccess.class)
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") Long id) {
        return mailService.removeById(id);
    }

    @ApiOperation(value = "发送邮件")
    @PostMapping(value = "/sendMail")
    @ResponseBody
    public ResultMessage sendMail(@RequestBody MailSuccess mailSuccess) {
        return mailService.sendMail(mailSuccess);
    }

    @ApiOperation(value = "项目检测")
    @GetMapping(value = "/checkProject")
    @ResponseBody
    public ResultMessage checkProject() {
        checkProjectService.checkProject();
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setContent("检测项目完成");
        return resultMessage;
    }

    @PostMapping(value = "findMailSuccess")
    @ApiOperation(value = "分页查询成功发送邮件信息  --by zhengyt")
    public com.cloud.logger.zchain.zchainserver.base.ResultMessage findMailSuccess(@RequestBody PagePo<MailSuccess> pageInfo) throws Exception {
        return com.cloud.logger.zchain.zchainserver.base.ResultMessage.success(mailService.findMailSuccess(pageInfo));
    }

    @PostMapping(value = "findMailFailure")
    @ApiOperation(value = "分页查询发送失败邮件信息  --by zhengyt")
    public com.cloud.logger.zchain.zchainserver.base.ResultMessage findMailFailure(@RequestBody PagePo<MailFailure> pageInfo) throws Exception {
        return com.cloud.logger.zchain.zchainserver.base.ResultMessage.success(mailService.findMailFailure(pageInfo));
    }

}